package com.example.noobpackage.ui.userinfo

import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.LiveData
import androidx.navigation.fragment.findNavController
import com.example.noobpackage.R
import com.example.noobpackage.logic.model.ApplicationUser
import com.example.noobpackage.logic.repository.ExpressRepository
import com.example.noobpackage.tool.LocalUserUtil
import com.example.noobpackage.tool.MyApplication
import com.example.noobpackage.ui.MainViewModel
import com.example.noobpackage.ui.util.LoginRequiredFragment
import kotlinx.android.synthetic.main.fragment_user_info.*

class UserInfoFragment : LoginRequiredFragment() {
    private val sharedViewModel: MainViewModel by activityViewModels()
    private lateinit var userInfo: ApplicationUser

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        userInfo = sharedViewModel.userInfo.value!!
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        setupReturn()
        return inflater.inflate(R.layout.fragment_user_info, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        loginJudge {
            userName.text = it?.name
            userAccountContent.text = it?.account.toString()
            userLocationContent.text = 'C' + it?.studentDepartment.toString()
            userPhoneContent.text = it?.telephoneNumber
            logoutBtn.setOnClickListener {
                Toast.makeText(context, "退出登录!", Toast.LENGTH_SHORT).show()
                val userInfoSharedPreferences: SharedPreferences =
                    MyApplication.context.getSharedPreferences(
                        "userInfo",
                        Context.MODE_PRIVATE
                    )
                userInfoSharedPreferences.edit().clear().apply()
                sharedViewModel.reloadUserInfo()
                sharedViewModel.reloadExpress()

            }
        }
    }


}